package com.fengwk.support.exception;

import java.util.HashMap;
import java.util.Map;

import com.fengwk.support.util.ResultUtils;

public class BaseException extends RuntimeException {

	private static final long serialVersionUID = 7393751402742546068L;

	private final static Map<Long, String> ERROR_CODE_MAP = new HashMap<Long, String>();

	protected long code = ResultUtils.FAIL_CODE;

	protected Map<String, Object> errorMap;

	static {
		registerErrorCodeMap(ResultUtils.UNKNOWN_ERROE_CODE, "未知错误");
		registerErrorCodeMap(ResultUtils.FAIL_CODE, "失败");
		registerErrorCodeMap(ResultUtils.SUCCESS_CODE, "成功");
		registerErrorCodeMap(ResultUtils.SYSTEM_ERROE_CODE, "系统错误");
		registerErrorCodeMap(ResultUtils.METHOD_DEFINE_CODE, "接口错误");
		registerErrorCodeMap(ResultUtils.PARAMETER_ERROR_CODE, "参数错误");
	}

	protected static void registerErrorCodeMap(Long errCode, String msg) {
		ERROR_CODE_MAP.put(errCode, msg);
	}

	public BaseException() {
		super();
	}

	public BaseException(String message) {
		super(message);
	}

	public BaseException(long code) {
		this(ERROR_CODE_MAP.get(code));
		this.code = code;
	}

	public BaseException(long code, String message) {
		this(message);
		this.code = code;
	}

	public BaseException(long code, String message, Map<String, Object> errorMap) {
		this(message);
		this.code = code;
		this.errorMap = errorMap;
	}

	public void setErrorAttribute(String key, Object value) {
		if (errorMap == null) {
			errorMap = new HashMap<String, Object>();
		}
		errorMap.put(key, value);
	}

	public void removeErrorAttribute(String key) {
		if (errorMap == null) {
			return;
		}
		errorMap.remove(key);
	}

	public void removeAllErrorAttribute() {
		if (errorMap == null) {
			return;
		}
		errorMap = null;
	}

	public long getCode() {
		return code;
	}

	public Map<String, Object> getErrorMap() {
		return errorMap;
	}
	
}
